package com.example.boot3.rpc.Service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Service
@Slf4j
public class WeatherService {
    @Value("${weather.appCode}")
    private String appCode;
    public Mono<String> getWeather(String area){
        //log.info("appCode="+this.appCode);
        WebClient client =WebClient.create();
        Mono<String> authorization = client.get()
                .uri("https://ali-weather.showapi.com/area-to-weather?area=" + area)
                .accept(MediaType.APPLICATION_JSON)
                .header("Authorization", this.appCode)
                .retrieve()
                .bodyToMono(String.class);//定义返回值类型

        return authorization;
    }

    class LoginRequest {
        private String loginName;
        private String password;

        public String getLoginName() {
            return loginName;
        }

        public void setLoginName(String loginName) {
            this.loginName = loginName;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }

    class LoginResponse {
        private String token;
        private String userId;
        private String message;

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }


    public Mono<LoginResponse> postWeather(){
        LoginRequest loginRequest =new LoginRequest();

            return WebClient.create().post()
                    .uri("/auth/login") // 相对路径，会拼接到 baseUrl 后面
                    .contentType(MediaType.APPLICATION_JSON) // 设置请求头 Content-Type: application/json
                    .bodyValue(loginRequest) // 设置请求体，WebClient 会自动序列化为 JSON
                    .retrieve() // 发起请求并获取响应
                    .bodyToMono(LoginResponse.class); // 将响应体反序列化为 LoginResponse 对象

    }
}
